{% extends "special_menu.html" %}
{% load i18n %}
{% block content %}
<form id="id_edit_form" action="" method="post">
    <table class="content_tbl" style='width:97%; margin-left:5px;'>
        <tbody>
        <tr><td><div class='div_box1 floatL' style='width:98%'><h2>{% trans '邮箱设置 ' %}</h2>
            <table id='tbl1'><tbody>
                <tr>
                    <th width="200px"><label for="id_email_host" class="required">{% trans '邮件服务器' %}:</label></th>
                    <td width="140px"><input id="id_email_host" name="email_host" class="required" type="text" value=""/></td>
                    <td><span>{% trans '(smtp.xxx.xxx)' %}</span></td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_port" class="required">{% trans '服务器端口' %}:</label></th>
                    <td width="140px"><input id="id_email_port" name="email_port" class="required" type="text" value=""/></td>
                    <td><input id='id_email_use_tls_checkbox' type="checkbox"/>{% trans 'SSL' %}</td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_host_user" class="required">{% trans '账户' %}:</label></th>
                    <td width="140px"><input id="id_email_host_user" name="email_host_user" class="required" type="text" value=""/></td>
                    <td>{% trans '(xxx@xxx.xxx)' %}</td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_email_host_password" class="required">{% trans '密码' %}:</label></th>
                    <td width="140px"><input id="id_email_host_password" name="email_host_password" class="required" type="password" value=""/></td>
                    <td></td>
                </tr>
                <tr>
                    <th width="200px"><label for="id_sender_name" class="required">{% trans '发送者名称' %}:</label></th>
                    <td width="140px"><input id="id_sender_name" name="sender_name" class="required" type="text" value="{{att_cfg.sender_name}}"/></td>
                    <td></td>
                </tr>
            </tbody></table>
        </div></td></tr>
        <tr><td><div class='div_box1 floatL' style='width:98%'><h2>{% trans '报警设置' %}</h2>
            <table id="tbl2"><tbody>
                <tr>
                    <th><label for="id_late_count" class="required">{% trans '迟到超过' %}</label></th>
                    <td><input id="id_late_count" name="late_count"  type="text"  value=""/></td>
                    <td style="margin-left: -10px"><span>{% trans '次,邮件提醒' %}</span></td>
                </tr>
                <tr>
                    <th><label for="id_early_count" class="required">{% trans '早退超过' %}</label></th>
                    <td><input id="id_early_count" name="early_count" type="text" value=""/></td>
                    <td><span>{% trans '次,邮件提醒 ' %}</span></td>
                </tr>
                <tr>
                    <th><label for="id_absent_count" class="required">{% trans '旷工超过' %}</label></th>
                    <td><input id="id_absent_count" name="absent_count" type="text" value=""/></td>
                    <td><span>{% trans '次,邮件提醒  ' %}</span></td>
                </tr>
                <tr>
                    <th><label for="id_send_rate" class="required">{% trans '邮件发送频率' %}:</label></th>
                    <td><select id="id_send_rate" name="send_rate">
                        <option value="0">{% trans '每月' %}</option>
                        <option value="1">{% trans '每周' %}</option>
                        <option value="2">{% trans '每日' %}</option>
                    </select>
                    </td>
                    <td></td>
                </tr>
                <tr>
                    <th><label for="id_exception_send" class="required">{% trans '考勤结果异常发送邮件' %}:</label></th>
                    <td><select id="id_exception_send" name="exception_send" >
                        <option value="0">{% trans '否' %}</option>
                        <option value="1">{% trans '是' %}</option>
                    </select>
                    </td>
                    <td></td>
                </tr>
            </tbody> </table>
        </div></td></tr>
        <tr><td colspan="3"><span id="id_error" style="display:none;"></span></td></tr>
        <tr>
            <td colspan="3">
                <div class='editformbtn'>
                    <div class='lineH22'>
                        <span class='action_OK'>&nbsp;</span>
                        <a id='id_submit'class='Link_blue1' href='javascript:void(0)' >{%trans '确定' %}</a>
                        <div class='clear'></div>
                    </div>
                </div>
            </td>
        </tr>
        </tbody>
    </table>
    <input id="id_email_use_tls" name="email_use_tls" type="hidden"/>
</form>
{% endblock %}
{% block getdatalist %}
{% endblock %}
{% block addjs %}
    function formValid(){
        <!--var email_user_check = ($("#id_email_host_user").val()).match("^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$");-->
        var email_user_check = ($("#id_email_host_user").val()).match("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
        if($("#id_email_host_user").val() != '' && email_user_check == null)
        {
            alert(gettext("邮箱地址格式不正确"));
            return false;
        }
        return true;
    };
    function initVal(){
        $.ajax({
            url:"/base/get_alarm_options/",
            type:"Get",
            dataType:"json",
            async: true,
            success: function(callback){
                if(callback){
                    var cfg = callback.cfg;
                    if(cfg.email_use_tls == 1){
                        $("#id_email_use_tls_checkbox").attr('checked', true);
                    }
                    $("#id_sender_name").val(cfg.sender_name);
                    $("#id_email_host").val(cfg.email_host);
                    $("#id_email_port").val(cfg.email_port);
                    $("#id_email_host_user").val(cfg.email_host_user);
                    $("#id_email_host_password").val(cfg.email_host_password);
                    $("#id_email_use_tls").val(cfg.email_use_tls);
                    $("#id_late_count").val(cfg.late_count);
                    $("#id_early_count").val(cfg.early_count);
                    $("#id_absent_count").val(cfg.absent_count);
                    $("#id_send_rate").val(cfg.send_rate);
                    $("#id_exception_send").val(cfg.exception_send);
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                alert(gettext("服务器处理数据失败，请重试！"));
            }
        });
    };
    function initFunction(){
        $('#id_edit_form').validate({
            rules:{
                "email_port":{required:true,digits:true,min:1,max:65535},
                "late_count":{required:true,digits:true,min:0},
                "early_count":{required:true,digits:true,min:0},
                "absent_count":{required:true,digits:true,min:0},
            },
            errorPlacement: function(error, element) {
                error.appendTo(element.parent());
            }
        });
        $("#id_email_use_tls_checkbox").click(function(){
            if($("#id_email_use_tls_checkbox").attr('checked')== true){
                $("#id_email_use_tls").val(1);
            }else{
                $("#id_email_use_tls").val(0);
            }
        });
        $('#id_submit').click(function(){
            $("#id_error").empty();
            if($("#id_edit_form").valid()){
                if(!formValid()){
                    return ;
                }
                $("#id_edit_form").ajaxSubmit({
                    url:"/base/set_alarm_options/",
                    type:"POST",
                    dataType:"json",
                    async: true,
                    success:function(callback){
                        $("#id_error").attr("style", "display:block");
                        if (callback.ret == 'ok')
                        {
                            $("#id_error").html('<ul class="successlist"><li><font color="blue">'+gettext("保存成功")+'</font></li></ul>');
                        }else
                        {
                            $("#id_error").html('<ul class="successlist"><li><font color="red">'+callback.ret+'</font></li></ul>');
                        }
                    },
                    error:function(XMLHttpRequest, textStatus, errorThrown){
                        alert(gettext("服务器处理数据失败，请重试！"));
                    }
                })
            }
        });
        initVal();
    };
    initFunction();
{% endblock %}
